/********************************************************************************
*                 Copyright (C) 2011 - Las Venturas Playground                  *
*                      http://iv-mp.nl | http://lvp-iv.com                      *
*                                                                               *
*    This program is free software: you can redistribute it and/or modify       *
*    it under the terms of the GNU General Public License as published by       *
*    the Free Software Foundation, either version 3 of the License, or          *
*    (at your option) any later version.                                        *
*                                                                               *
*    This program is distributed in the hope that it will be useful,            *
*    but WITHOUT ANY WARRANTY; without even the implied warranty of             *
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
*    GNU General Public License for more details.                               *
*                                                                               *
*    You should have received a copy of the GNU General Public License          *
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.      *
*                                                                               *
*********************************************************************************
*                                                                               *
*  Las Venturas Playground - callbacks/onPlayerSpawn.nut                        *
*                                                                               *
*  The onPlayerSpawn callback is located here.                                  *
*                                                                               *
********************************************************************************/

/**
 * This event is called when a user spawns.
 *
 * @param playerid The playerid that spawned.
 */
function onPlayerSpawn (playerid)
{
    // Set this variable to true so he can use commands again.
    isSpawned[playerid] = true;
    
    // Give the user a pistol to get him or her started.
    givePlayerWeapon (playerid, 7, 999);
    
    // The user is registered but not logged in.
    if(isRegistered[playerid] == true && isLoggedIn[playerid] == false)
    {   
        // Todo: Teleport to some login room.
        // Freeze the user and prompt him to login.
        togglePlayerFrozen (playerid, true);
        
        sendPlayerMessage (playerid, "This account is registered. Please /login to login onto your account.", COLOR_YELLOW);
        sendPlayerMessage (playerid, "If this is not your account, please quit and rejoin with another name.", COLOR_YELLOW);
    }
    else
    {
        // If the skin isn't -1 (aka none saved), set his model.
        if (playerInfo[playerid].Skin != -1)
        {
            setPlayerModel (playerid, playerInfo[playerid].Skin);
        }    
        
        // Get a random array element.
        local randomIndex = random (0, (spawnPositions.len () - 1));
        // Set a random spawn position.
        setPlayerCoordinates (playerid, spawnPositions[randomIndex].x, spawnPositions[randomIndex].y, spawnPositions[randomIndex].z);
        // And set the user's heading.
        setPlayerHeading (playerid, spawnPositions[randomIndex].heading);
    }
    return 1;
}

addEvent("playerSpawn", onPlayerSpawn);